package com.yuan.dingtalk.service;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.taobao.api.ApiException;
import com.yuan.dingtalk.constant.ApiConstant;
import com.yuan.dingtalk.vo.UserInfo;
import org.springframework.beans.BeanUtils;

/**
 * @version 1.0
 * @Author:wangy
 * @Date: 2020/04/20 15:16
 **/
public abstract class AbstractDingtalkService implements IDingtalkService {
    /**
     * apiUrl
     */
    private static String apiUrl = null;

    public AbstractDingtalkService(String apiUrl) {
        this.apiUrl = apiUrl;
    }

    /**
     * 获取缓存中的access_token
     * @param appKey
     * @param appSecret
     * @return
     * @throws ApiException
     */
    @Override
    public abstract String getCacheAccessToken(String appKey, String appSecret) throws ApiException;

    @Override
    public String getUserId(String access_token, String code) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(apiUrl + "/user/getuserinfo");
        OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
        request.setCode(code);
        request.setHttpMethod("GET");
        OapiUserGetuserinfoResponse response = client.execute(request, access_token);
        if(response.isSuccess()){
            return response.getUserid();
        }
        throw new ApiException(response.getMsg());
    }

    @Override
    public UserInfo getUserInfo(String access_token, String userid, String lang) throws ApiException {
        UserInfo userInfo=new UserInfo();
        DingTalkClient client = new DefaultDingTalkClient(apiUrl + "/user/get");
        OapiUserGetRequest request = new OapiUserGetRequest();
        request.setUserid(userid);
        request.setHttpMethod("GET");
        OapiUserGetResponse response = client.execute(request, access_token);
        if(response.isSuccess()){
            BeanUtils.copyProperties(response, userInfo);
            return userInfo;
        }
        throw new ApiException(response.getMsg());
    }

    /**
     * 获取钉钉access_token
     * @param appKey
     * @param appSecret
     * @return
     * @throws ApiException
     */
    @Override
    public OapiGettokenResponse getPrototypeTokenResponse(String appKey, String appSecret) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(apiUrl+ApiConstant.GET_TOKEN);
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appKey);
        request.setAppsecret(appSecret);
        request.setHttpMethod("GET");
        return client.execute(request);
    }
    /**
     * 获取钉钉原生access_token
     * @param appKey
     * @param appSecret
     * @return
     * @throws ApiException
     */
    public String getPrototypeAccessToken(String appKey, String appSecret) throws ApiException{
        OapiGettokenResponse tokenResponse = this.getPrototypeTokenResponse(appKey, appSecret);
        if(tokenResponse.isSuccess()){
            return tokenResponse.getAccessToken();
        }
        throw new ApiException(tokenResponse.getMsg());
    }
}
